﻿//
// Copyright (c) 2024 BPer
//
#ifndef BPERWEBRTC_BPERRTCRTP_H_
#define BPERWEBRTC_BPERRTCRTP_H_

#include <bperutil/buffer/BPerBuffer.h>
#include <bperutil/sys/BPerMath.h>

typedef enum BPerRtpPacketPayloadType {
	BPerRtpPacketPayloadTypeRaw,
	BPerRtpPacketPayloadTypeFUA2,
	BPerRtpPacketPayloadTypeFUA,
	BPerRtpPacketPayloadTypeNALU,
	BPerRtpPacketPayloadTypeSTAP,
	BPerRtpPacketPayloadTypeUnknown,
} BPerRtpPacketPayloadType;

typedef enum BPerRtpExtensionType {
	kRtpExtensionNone,
	kRtpExtensionTransportSequenceNumber,
	kRtpExtensionAudioLevel,
	kRtpExtensionNumberOfExtensions
} BPerRtpExtensionType;

// Fast parse the SSRC from RTP packet. Return 0 if invalid.
uint32_t bPer_rtp_fast_parse_ssrc(char *buf, int32_t size);
uint8_t bPer_rtp_fast_parse_pt(char *buf, int32_t size);
int32_t bPer_rtp_fast_parse_twcc(char *buf, int32_t size, uint8_t twcc_id,
		uint16_t *twcc_sn);

BPerBool bPer_seq_is_newer(uint16_t value, uint16_t pre_value);
BPerBool bPer_seq_is_rollback(uint16_t value, uint16_t pre_value);
int32_t bPer_seq_distance(uint16_t value, uint16_t pre_value);

typedef struct BPerRtpExtensionTypes {
	uint8_t ids[kRtpExtensionNumberOfExtensions];
} BPerRtpExtensionTypes;
typedef struct BPerRtpExtensionTwcc {
	BPerBool has_twcc;
	uint8_t id;
	uint16_t sn;
} BPerRtpExtensionTwcc;

typedef struct BPerRtpExtensionOneByte {
	BPerBool has_ext;
	int32_t id;
	uint8_t value;
} BPerRtpExtensionOneByte;

struct BPerRtpExtension {
	BPerBool has_ext;
	BPerBool decode_twcc_extension;
};

typedef struct BPerRtpExtensions {
	BPerBool has_ext;

	BPerBool decode_twcc_extension;
	BPerRtpExtensionTwcc twcc;
	BPerRtpExtensionOneByte audio_level;
    BPerRtpExtensionTypes *types;

} BPerRtpExtensions;
#ifdef __cplusplus
extern "C"{
#endif
int32_t bPer_encode_rtpExtensions(BPerRtpExtensions *ext, BPerBuffer *buf);
int32_t bPer_encode_rtpExtensionsTwcc(BPerRtpExtensionTwcc *twcc,BPerBuffer *buf);
int32_t bPer_encode_rtpExtensionsOneByte(BPerRtpExtensionOneByte *one,BPerBuffer *buf);

int32_t bPer_decode_rtpExtensions(BPerRtpExtensions *ext, BPerBuffer *buf);
int32_t bPer_decode_rtpExtensionsTwcc(BPerRtpExtensionTwcc *twcc,BPerBuffer *buf);
int32_t bPer_decode_rtpExtensionsOneByte(BPerRtpExtensionOneByte *one,BPerBuffer *buf);
uint64_t bPer_rtpExtensionTwcc_nb(BPerRtpExtensionTwcc *twcc);
uint64_t bPer_rtpExtensionOneByte_nb(BPerRtpExtensionOneByte *onebyte);
#ifdef __cplusplus
}
#endif


#endif /* BPERWEBRTC_BPERRTCRTP_H_ */
